package com.masterclient.app.modules.income.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Window;
import android.view.WindowManager;
import com.eoner.baselibrary.utils.ScreenUtils;
import com.masterclient.app.R;
import com.masterclient.app.modules.income.dealer.model.DealerBankCardBean;
import com.masterclient.app.modules.income.adapter.SelectBankCardAdapter;
import java.util.List;

public class SelectBankCardDialog extends Dialog {

    private final Activity mContext;
    List<DealerBankCardBean> list;
    List<DealerBankCardBean> newList;
    private int type;
    private String bankCardCertId;

    public SelectBankCardDialog(@NonNull Activity context, List<DealerBankCardBean> list,String bankCardCertId) {
        super(context, R.style.MyDialog);
        this.list = list;
        this.newList = list;
        this.bankCardCertId = bankCardCertId;
        mContext = context;
    }

    public void updateBankCardCertId(String bankCardCertId){
        this.bankCardCertId = bankCardCertId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_select_bank_card);
        setCanceledOnTouchOutside(true);

        RecyclerView recyclerView = findViewById(R.id.recyclerView);
        SelectBankCardAdapter selectBankCardAdapter = new SelectBankCardAdapter(mContext, list, bankCardCertId);
        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        recyclerView.setAdapter(selectBankCardAdapter);
        if (list.size() < 10) {
            type = 1;
            newList.add(new DealerBankCardBean());
            selectBankCardAdapter.setType(1);
        }else {
            type = 2;
            selectBankCardAdapter.setType(2);
        }
        selectBankCardAdapter.setNewData(newList);
        selectBankCardAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (type == 1) {
                if (newList.size() - 1 == position) {
                    onBankCardClickListener.addBankClickListener();
                } else {
                    onBankCardClickListener.bankCardClickListener(newList.get(position).bankShort, newList.get(position).card, newList.get(position).bankcardCertId);
                }
            } else {
                onBankCardClickListener.bankCardClickListener(newList.get(position).bankShort, newList.get(position).card, newList.get(position).bankcardCertId);
            }
        });

        findViewById(R.id.tv_cancel).setOnClickListener(view -> dismiss());
    }

    @Override
    protected void onStart() {
        super.onStart();
        Window window = getWindow();
        window.setWindowAnimations(R.style.bottomShow);
        window.setGravity(Gravity.BOTTOM);

        WindowManager wm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
        WindowManager.LayoutParams windowParams = window.getAttributes();
        DisplayMetrics metrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(metrics);
        windowParams.width = ScreenUtils.getScreenWidth();
        windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(windowParams);
        window.setBackgroundDrawableResource(android.R.color.transparent);
    }

    public interface OnBankCardClickListener {
        void bankCardClickListener(String bankShortName, String bankCode, String bankcardCertId);
        void addBankClickListener();
    }

    private OnBankCardClickListener onBankCardClickListener;

    public void setOnButtonClickListener(OnBankCardClickListener onBankCardClickListener) {
        this.onBankCardClickListener = onBankCardClickListener;
    }
}
